<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "order".
 *
 * @property integer $order_id
 * @property string $oid
 * @property integer $feesum
 * @property string $create_time
 * @property integer $status
 * @property integer $goods_id
 * @property string $title
 * @property integer $userid
 * @property string $username
 * @property string $usermobile
 * @property string $address
 * @property string $memo
 * @property string $memo_reply
 */
class MOrder extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['oid', 'feesum', 'status', 'goods_id', 'title', 'userid', 'username', 'usermobile', 'address', 'memo', 'memo_reply'], 'required'],
            [['feesum', 'status', 'goods_id', 'userid'], 'integer'],
            [['create_time'], 'safe'],
            [['oid', 'username'], 'string', 'max' => 32],
            [['title', 'address'], 'string', 'max' => 128],
            [['usermobile'], 'string', 'max' => 16],
            [['memo', 'memo_reply'], 'string', 'max' => 256],
            [['count'], 'integer'],
            [['starttime'], 'string', 'max' => 64],
            [['endtime'], 'string', 'max' => 64],
            /*收货人信息*/
            [['receiver_name'], 'string', 'max' => 32],
            [['receiver_address'], 'string', 'max' => 128],
            [['receiver_mobile'], 'string', 'max' => 16],
            [['receiver_memo'], 'string', 'max' => 128],
            /*开发票信息*/
            [['invoice_type'], 'integer'],
            [['invoice_company'], 'string', 'max' => 128],
            [['invoice_bank'], 'string', 'max' => 128],
            [['invoice_receivernumber'], 'string', 'max' => 64],
            [['invoice_address'], 'string', 'max' => 128],
            [['invoice_mobile'], 'string', 'max' => 16],
            [['invoice_memo'], 'string', 'max' => 128],


             /*客服信息 Customer Service*/
            [['cs_name'], 'string', 'max' => 32],
            [['cs_contact'], 'string', 'max' => 128],

            /*出库物流*/
            [['checkout_company'], 'string', 'max' => 64],
            [['checkout_id'], 'string', 'max' => 64],

            /*返程物流*/
            [['checkin_company'], 'string', 'max' => 64],
            [['checkin_id'], 'string', 'max' => 64],

            /*财务信息*/
            [['finance_deposit'], 'integer'],
            [['finance_transaction_price'], 'integer'],
            [['finance_memo'], 'string', 'max' => 128],


        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => '订单编号',
            'oid' => '订单号',
            'feesum' => '价格',
            'create_time' => '创建时间',
            'status' => '状态',
            'goods_id' => '商品编号',
            'title' => '商品名',
            'userid' => '用户编号',
            'username' => '用户名',
            'usermobile' => '手机',
            'address' => '地址',
            'memo' => '用户备注',
            'memo_reply' => '回复',

            'count' => '数量',
            'starttime' => '租借起始时间',
            'endtime' => '租借结束时间',

            /*收货人信息*/
            'receiver_name' => '收货人姓名',
            'receiver_address' => '收货人地址',
            'receiver_mobile' => '收货人电话',
            'receiver_memo' => '收货人备注',
            /*开发票信息*/
            'invoice_type' => '发票',
            'invoice_company' => '单位名称',
            'invoice_bank' => '开户行',
            'invoice_receivernumber' => '税号',
            'invoice_address' => '地址',
            'invoice_mobile' => '电话',
            'invoice_memo' => '备注',
            /*客服信息 Customer Service*/
            'cs_name' => '客服姓名',
            'cs_contact' => '联系方式',
            /*出库物流*/
            'checkout_company' => '出库物流公司名称',
            'checkout_id' => '出库物流单号',

            /*返程物流*/
            'checkin_company' => '返程物流公司名称',
            'checkin_id' => '返程物流单号',

            /*财务信息*/
            'finance_deposit' => '租借人交押金',
            'finance_transaction_price' => '最终成交价格',
            'finance_memo' => '财务信息备注',

        ];
    }

    //订单状态 （订单状态流转 需考虑）
    static function getStatusOption($key=null)
    {
        $arr = array(
            1 => '订单产生',
            2 => '客服处理',
            3 => '物流已发送',
            4 => '租借人已签收',
            5 => '外借',
            6 => '返程物流',
            7 => '出借人已签收',
            8 => '入库',
        );

        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

    static function getStatus($model)
    {
        return self::getStatusOption($model->status);
    }

    static function getInvoiceTypeOption($key=null)
    {
        $arr = array(
            1 => '不需要发票',
            2 => '增值税普通发票（租金加17% ）',
            3 => '增值税发票（租金加17%）',
        );

        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

    static function getInvoiceType($model)
    {
        return self::getInvoiceTypeOption($model->invoice_type);
    }


    static function getPubUserInfo($model,$item)
    {
        $goods = \app\models\MGoods::findOne(['goods_id' => $model->goods_id]);
        if(!empty($goods))
        {
            $user = \app\models\MUser::findOne(['id' => $goods->pub_userid]);
            if(empty($user))
                return '-';
        }
        else
            return '-';

        if($item == 'name')
        {
            return $user->username;
        }
        else if($item == 'mobile')
        {
            return $user->mobile;
        }
        else
            return '-';
    }

    static function getCheckoutInfo($model)
    {

        if(!empty($model->checkout_id))
        {
            $checout_info = "<a target='_blank' href='http://m.kuaidi100.com/result.jsp?nu=".$model->checkout_id."' >查看物流情况</a>";
        }
        else
            $checout_info = "";

        return $model->checkout_id."&nbsp;&nbsp;".$checout_info;

    }


}
